<?php

/**
* alltosun.com 接口统计 api_stat.php
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 钱有明 (qianym@alltosun.com) $
* $Date: 2015-7-3 上午10:05:33 $
* $Id$
*/

class api_stat_widget
{
    /**
     * 保存接口请求log
     * @param array $info
     * @return int
     */
    public function record($info)
    {
        if (!$info) {
            return 0;
        }

        return _model('api_stat')->create($info);
    }

    /**
     * 更新接口请求log
     * @param int $id
     * @param array $info
     * @return boolean
     */
    public function update_record($id, $info)
    {
        if (!$id || !$info) {
            return false;
        }

        return !!_model('api_stat')->update($id, $info);
    }

    /**
     * 获取经销商销售数据
     * @param  [type] $dealer_id 经销商的UID
     * @param  string $type     获取的时间段  all所有   curr_month当前月   prev_month上一个月   curr_day当前天   prev_day上一天
     * @param  string $get_type 获取的数据类型    price 额度    box 箱数
     * @return [type]           [description]
     */
    public function get_dealer_sale_data($dealer_id, $type = 'all', $get_type = 'price')
    {
        $filter = array(
            'dealer_id' => $dealer_id
            );
        $time = time();
        if ($type == 'curr_month') {
            $curr_month = date('m', $time);
            $start_time = date('Y-m-01 00:00:00', $time);
            $end_time = date('Y-m-d H:i:s', $time);
            $filter['add_time >='] = $start_time;
            $filter['add_time <'] = $end_time;
        } elseif ($type == 'prev_month') {
            $start_time = date("Y-m-d H:i:s", mktime(0, 0 , 0,date("m") - 1, 1, date("Y")));
            $end_time = date("Y-m-d H:i:s", mktime(23,59,59,date("m"), 0, date("Y")));
            $filter['add_time >='] = $start_time;
            $filter['add_time <'] = $end_time;
        } elseif ($type == 'curr_day') {
            $start_time = date("Y-m-d 00:00:00:", $time);
            $end_time = date("Y-m-d H:i:s", $time);
            $filter['add_time >='] = $start_time;
            $filter['add_time <'] = $end_time;
        } elseif ($type == 'prev_day') {
            $start_time = date("Y-m-d 00:00:00:", strtotime('-1 day'));
            $end_time = date("Y-m-d 59:59:59", strtotime('-1 day'));
            $filter['add_time >='] = $start_time;
            $filter['add_time <'] = $end_time;
        } else {

        }

        $return_data = _model('dealer_sale_record')->getList($filter);
        $count_price = 0;
        $count_box = 0;
        foreach ($return_data as $key => $value) {
            $count_price += $value['price'];
            $count_box += $value['box'];
        }
        if($get_type == 'price') {
            return $count_price;
        } elseif ($get_type == 'box') {
            return $count_box;
        } else {
            return 0;
        }
    }

    /**
     * 获取终端销售数据
     * @param  [type] $store_id  终端的门店ID
     * @param  string $type     获取的时间段  all所有   curr_month当前月   prev_month上一个月   curr_day当前天   prev_day上一天
     * @param  string $get_type 获取的数据类型    price 额度    box 箱数
     * @return [type]           [description]
     */
    public function get_store_sale_data($store_id, $type = 'all', $get_type = 'price')
    {
        $filter = array(
            'store_id' => $store_id
            );
        $time = time();
        if ($type == 'curr_month') {
            $curr_month = date('m', $time);
            $start_time = date('Y-m-01 00:00:00', $time);
            $end_time = date('Y-m-d H:i:s', $time);
            $filter['add_time >='] = $start_time;
            $filter['add_time <'] = $end_time;
        } elseif ($type == 'prev_month') {
            $start_time = date("Y-m-d H:i:s", mktime(0, 0 , 0,date("m") - 1, 1, date("Y")));
            $end_time = date("Y-m-d H:i:s", mktime(23,59,59,date("m"), 0, date("Y")));
            $filter['add_time >='] = $start_time;
            $filter['add_time <'] = $end_time;
        } elseif ($type == 'curr_day') {
            $start_time = date("Y-m-d 00:00:00:", $time);
            $end_time = date("Y-m-d H:i:s", $time);
            $filter['add_time >='] = $start_time;
            $filter['add_time <'] = $end_time;
        } elseif ($type == 'prev_day') {
            $start_time = date("Y-m-d 00:00:00:", strtotime('-1 day'));
            $end_time = date("Y-m-d 59:59:59", strtotime('-1 day'));
            $filter['add_time >='] = $start_time;
            $filter['add_time <'] = $end_time;
        } else {

        }

        $return_data = _model('dealer_sale_record')->getList($filter);
        $count_price = 0;
        $count_box = 0;
        foreach ($return_data as $key => $value) {
            $count_price += $value['price'];
            $count_box += $value['box'];
        }
        if($get_type == 'price') {
            return $count_price;
        } elseif ($get_type == 'box') {
            return $count_box;
        } else {
            return 0;
        }

    }

    /**
     * 获取总店销售数据
     * @param  [type] $store_id  总店的UID
     * @param  string $type     获取的时间段  all所有   curr_month当前月   prev_month上一个月   curr_day当前天   prev_day上一天
     * @param  string $get_type 获取的数据类型    price 额度    box 箱数
     * @return [type]           [description]
     */
    public function get_owner_sale_data($owner_id, $type = 'all', $get_type = 'price')
    {
        $filter = array(
            'owner_id' => $owner_id
            );
        $time = time();
        if ($type == 'curr_month') {
            $curr_month = date('m', $time);
            $start_time = date('Y-m-01 00:00:00', $time);
            $end_time = date('Y-m-d H:i:s', $time);
            $filter['add_time >='] = $start_time;
            $filter['add_time <'] = $end_time;
        } elseif ($type == 'prev_month') {
            $start_time = date("Y-m-d H:i:s", mktime(0, 0 , 0,date("m") - 1, 1, date("Y")));
            $end_time = date("Y-m-d H:i:s", mktime(23,59,59,date("m"), 0, date("Y")));
            $filter['add_time >='] = $start_time;
            $filter['add_time <'] = $end_time;
        } elseif ($type == 'curr_day') {
            $start_time = date("Y-m-d 00:00:00:", $time);
            $end_time = date("Y-m-d H:i:s", $time);
            $filter['add_time >='] = $start_time;
            $filter['add_time <'] = $end_time;
        } elseif ($type == 'prev_day') {
            $start_time = date("Y-m-d 00:00:00:", strtotime('-1 day'));
            $end_time = date("Y-m-d 59:59:59", strtotime('-1 day'));
            $filter['add_time >='] = $start_time;
            $filter['add_time <'] = $end_time;
        } else {

        }

        $return_data = _model('dealer_sale_record')->getList($filter);
        $count_price = 0;
        $count_box = 0;
        foreach ($return_data as $key => $value) {
            $count_price += $value['price'];
            $count_box += $value['box'];
        }
        if($get_type == 'price') {
            return $count_price;
        } elseif ($get_type == 'box') {
            return $count_box;
        } else {
            return 0;
        }

    }

    /**
     * 获取市场人员销售数据
     * @param  [type] $store_id 市场人员的UID
     * @param  string $type     获取的时间段  all所有   curr_month当前月   prev_month上一个月   curr_day当前天   prev_day上一天
     * @param  string $get_type 获取的数据类型    price 额度    box 箱数
     * @return [type]           [description]
     */
    public function get_marketing_sale_data($marketing_id, $type = 'all', $get_type = 'price')
    {
        $filter = array(
            'marketing_id' => $marketing_id
            );
        $time = time();
        if ($type == 'curr_month') {
            $curr_month = date('m', $time);
            $start_time = date('Y-m-01 00:00:00', $time);
            $end_time = date('Y-m-d H:i:s', $time);
            $filter['add_time >='] = $start_time;
            $filter['add_time <'] = $end_time;
        } elseif ($type == 'prev_month') {
            $start_time = date("Y-m-d H:i:s", mktime(0, 0 , 0,date("m") - 1, 1, date("Y")));
            $end_time = date("Y-m-d H:i:s", mktime(23,59,59,date("m"), 0, date("Y")));
            $filter['add_time >='] = $start_time;
            $filter['add_time <'] = $end_time;
        } elseif ($type == 'curr_day') {
            $start_time = date("Y-m-d 00:00:00:", $time);
            $end_time = date("Y-m-d H:i:s", $time);
            $filter['add_time >='] = $start_time;
            $filter['add_time <'] = $end_time;
        } elseif ($type == 'prev_day') {
            $start_time = date("Y-m-d 00:00:00:", strtotime('-1 day'));
            $end_time = date("Y-m-d 59:59:59", strtotime('-1 day'));
            $filter['add_time >='] = $start_time;
            $filter['add_time <'] = $end_time;
        } else {

        }

        $return_data = _model('marketing_sale_record')->getList($filter);
        $count_price = 0;
        $count_box = 0;
        foreach ($return_data as $key => $value) {
            $count_price += $value['price'];
            $count_box += $value['box'];
        }
        if($get_type == 'price') {
            return $count_price;
        } elseif ($get_type == 'box') {
            return $count_box;
        } else {
            return 0;
        }

    }
}
?>